package com.alibaba.controller;

import com.alibaba.model.entity.Goods;
import com.alibaba.model.param.GoodsParam;
import com.alibaba.service.GoodsService;
import com.alibaba.utils.Result;
import jakarta.annotation.Resource;
import lombok.NoArgsConstructor;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author ZhouYaDong
 * @date 2025/9/28
 * @description
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 根据ID查询商品详情
     */
    @GetMapping("/getGoodsById")
    public Result<Goods> getGoodsById(@RequestParam("id") String id) {
        val goods = goodsService.getGoodsById(Long.valueOf(id));
        return Result.success(goods);
    }

    /**
     * 更新商品信息
     */
    @PostMapping("/updateGoods")
    public Result<String> updateGoods(@RequestBody @Validated(GoodsParam.Update.class) GoodsParam goodsParam) {
        val goods = new Goods();
        BeanUtils.copyProperties(goodsParam, goods);
        val updated = goodsService.updateGoods(goods);
        if (!updated) {
            return Result.failure("Update failed: goods not found");
        }

        return Result.success("Goods updated successfully");
    };

    /**
     * 新增商品
     */
    @PostMapping("/addGoods")
    public Result<String> addGoods(@RequestBody @Validated(GoodsParam.Add.class) GoodsParam goodsParam) {
        val goods = new Goods();
        BeanUtils.copyProperties(goodsParam, goods);
        goodsService.addGoods(goods);
        return Result.success("Goods added successfully");
    };

    /**
     * 查询布隆过滤器中的所有商品ID
     */
    @GetMapping("/getAllGoodsIdsInBloomFilter")
    public Result<List<String>> getAllGoodsIdsInBloomFilter() {
        val ids = goodsService.listGoodsInBloomFilter();
        return Result.success(ids);
    }

}
